//
//  ByCellRedGrant.m
//  mijifen_ios
//
//  Created by mc on 2017/10/26.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellRedGrant.h"

// other
#import "ByViewMemberStoreMode.h"

@implementation ByCellRedGrant
{
    UIView      *_bgView;
    UILabel     *_titleLabel;
    UILabel     *_typeLabel;
    UIImageView *_rightImg;
    NSString    *_title;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView andTitle:(NSString *) title{
    
    static NSString *cellIdentifier = @"ByCellRedGrant";
    ByCellRedGrant *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andTitle:title];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitle:(NSString *) title{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        _title = title;
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(15));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30, 50));
    }];
    _bgView.backgroundColor = [UIColor whiteColor];
    
    _titleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_titleLabel];
    CGFloat width = [_title getSizeOfString:16].width + 5;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(10));
        make.left.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _titleLabel.font = FONT(16);
    _titleLabel.text = _title;

    _rightImg = [[UIImageView alloc] init];
    [_bgView addSubview:_rightImg];
    [_rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    _rightImg.image = [UIImage imageNamed:@"Conjuration_arrow_down"];

    _typeLabel = [[UILabel alloc] init];
    [_bgView addSubview:_typeLabel];
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(10);
        make.top.equalTo(@(10));
        make.right.equalTo(_rightImg.mas_left).offset(-10);
        make.bottom.equalTo(@(-10));
    }];
    _typeLabel.font = FONT(16);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(grantAction)];
    [_bgView addGestureRecognizer:tap];
}

- (void)grantAction{
    ByViewMemberStoreMode *modeView = [[ByViewMemberStoreMode alloc] init];
    modeView.modeArray = @[@"指定会员",@"30天内非活跃会员"];
    modeView.didSelectBlock = ^(NSString *str) {
        _typeLabel.text = str;
        if (self.grantBlock) {
            self.grantBlock(str);
        }
    };
    [modeView show];
    
    // 通知隐藏键盘
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideAimKeyBoard" object:nil];
}

@end
